#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int pipefd[2] = {0};
    int ret = pipe(pipefd);
    if(ret < 0){
        perror("pipe error");
        return -1;
    }
    pid_t pid = fork();
    if(pid < 0){
        perror("fork error");
    }else if(pid == 0){
        //子进程
        char buf[1024] = {0};
        int ret = read(pipefd[0],buf,1023);
        printf("buf:[%s]-[%d]\n", buf, ret);
        printf("buf:[%s]-[%d]\n", buf, ret);
        printf("buf:[%s]-[%d]\n", buf, ret);
        printf("buf:[%s]-[%d]\n", buf, ret);
    }else{
        //父进程
        char *ptr = "今天天气好晴朗~~";
        char *tmp = "in the end~~";
        int ret = 0;
        while(ret < 100)
        {
            write(pipefd[1], ptr, strlen(ptr));
            ret += 1;
        }
        write(pipefd[1], tmp, strlen(tmp));
        printf("写完了~~\n");
    }
    /*
    while(1)
    {
        printf("------------%d\n",getpid());
        sleep(1);
    }
    */
    return 0;
}
